﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class adm_vlg_loginForm : System.Web.UI.Page
{
    VLGDataContext _dba = new VLGDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {

        //if (Session["NhanVien"] != null)
        //{
        //    Response.Redirect("gig.aspx");

        //}

    }
    protected void btn_dn_Click(object sender, EventArgs e)
    {
        string usr = txtU.Text.Trim().ToString();
        string pasw = AllCode.Utils.Mahoa(txtP.Text.Trim().ToString());
        try
        {
            VLGAccount ac = _dba.VLGAccounts.Single(cp => cp.UsernameE == usr && cp.Password == pasw);
            if (ac.idGroup == "admin")
            {
                Session["NhanVien"] = ac;
                //CHAY CAC HAM HUY DON HANG KHONG DUOC ACTIVE SAU 1 THOI GIAN
                //CHAY HAM HOAN THANH DON HANG KHI BUYER KHONG XAC NHAN SAU 1 THOI GIAN
                //CHAY CAC HAM TRA TIEN VAO TAI KHOAN KHI YEU CAU THANH TOAN KHONG DUOC KICH HOAT VA VUOT QUA
                //1 THOI GIAN
                AutoCancelOrderNotActive();
                AutoCompleteOrder();
                AutoReturnBalance();
                Session["AID"] = ac.idAcc;
                // quay ve trang truoc đó
                if (Request.QueryString["returnURL"] != null)
                {
                    Response.Redirect(Request.QueryString["returnURL"]);
                   
                   
                }
                else
                {
                    Response.Redirect("gig.aspx");
                   
                }

            }
            else
            {
                Label1.Text = "Bạn không phải quản trị viên";
            }

        }
        catch 
        {
            Label1.Text = "Đăng nhập thất bại";
        }


    }

    //HỦY ĐƠN HÀNG SAU KHI HET THOI GIAN 3 NGAY VAN CHUA XÁC NHẬN QUA EMAIL
    public void AutoCancelOrderNotActive()
    {
        var list = from o in _dba.Orders
                   where o.CustomerActive != true && o.CustomerCancel != true && o.SellerCancel != true
                   select o;
        foreach (var item in list)
        {
            //TimeSpan current = (item.NgayDat.Value).AddDays(2) - DateTime.Now;
            TimeSpan current = item.NgayDat.Value.AddDays(3) - DateTime.Now;
            if (current.Hours < 0)
            {
                item.CustomerCancel = true;
                item.SellerCancel = true;
                item.dateSellerCancel = DateTime.Now;
                item.dateCustomerCancel = DateTime.Now;
                var acc = (from a in _dba.VLGAccounts
                           where a.idAcc == item.idAcc
                           select a).Single();
                //thanh toán paypal --> tra so tien paypal
                if (item.Type == 0 || item.Type == 2)
                {
                    if (acc.soTienPaypal.HasValue)
                        acc.soTienPaypal = acc.soTienPaypal + 5;
                    else acc.soTienPaypal = 5;
                }
                //thanh toán truc tiep --> tra so du vao so tien
                else
                {
                    if (acc.soTien.HasValue)
                        acc.soTien = acc.soTien + 5;
                    else
                        acc.soTien = 5;
                }
                _dba.SubmitChanges();

            }
        }
    }

    //HOÀN THÀNH ĐƠN HÀNG SAU KHI HET THOI GIAN 3 NGAY VAN CHUA XÁC NHẬN 
    public void AutoCompleteOrder()
    {
        var list = from o in _dba.Orders
                   where (o.CustomerConfirm != true && o.SellerReconfirm == true && o.SellerReconfirm != true && o.CustomerReject != true)
                   || (o.CustomerConfirm != true && o.SellerReconfirm == true && o.CustomerReject == true)
                   select o;
        foreach (var item in list)
        {
            //hoan thanh reconfirm trước
            if (item.dateSellerReconfirm.HasValue)
            {
                TimeSpan current = item.dateSellerReconfirm.Value.AddDays(3) - DateTime.Now;
                if (current.Hours < 0)
                {
                    item.CustomerConfirm = true;
                    item.NgayCustomerConfirm = DateTime.Now;
                    _dba.SubmitChanges();

                }
            }
            else
            {
                TimeSpan current = item.NgaySellerConfirm.Value.AddDays(3) - DateTime.Now;
                if (current.Hours < 0)
                {
                    item.CustomerConfirm = true;
                    item.NgayCustomerConfirm = DateTime.Now;
                    _dba.SubmitChanges();

                }
            }
        }
    }

    //TU DONNG TRA LAI TIEN CHO SELLER KHI QUA THOI GIAN 3 NGAY MA CHUA KICH HOAT MAIL XAC NHAN RUT TIEN
    //CHI CAP NHAT CAC GIA TRI CO RETURNBALANCE=0 VÀ NGÀY GUI YEU CAU VUOT QUA 3 NGAY SO VOI HIEN TAI
    public void AutoReturnBalance()
    {
        var yeuCauThanhToan = from y in _dba.YeuCauChuyenKhoans
                              where y.returnBalance != true && (y.ngay.Value.AddDays(3) - DateTime.Now).TotalMinutes < 0
                              select y;
        foreach (var item in yeuCauThanhToan)
        {
            var acc = (from a in _dba.VLGAccounts
                       where a.idAcc == item.idAcc
                       select a).Single();
            acc.soTien = acc.soTien + item.soTien;
            item.returnBalance = true;
            item.dateReturnBalance = DateTime.Now;
            _dba.SubmitChanges();
        }
    }
}
